package routes

import (
	"fmt"
	"net/http"
	"news/controllers"
	_ "news/docs"
	"news/logger"
	"news/middlewares"

	"github.com/gin-gonic/gin"
	gs "github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"
)

func Setup(mode string) *gin.Engine {
	if mode == gin.ReleaseMode {
		gin.SetMode(gin.ReleaseMode)
	}
	if err := controllers.InitTrans("zh"); err != nil {
		fmt.Printf("init trans failed, err:%v\n", err)
	}

	// 路由部份
	r := gin.New()
	r.Use(logger.GinLogger(), logger.GinRecovery(true))
	r.GET("/swagger/*any", gs.WrapHandler(swaggerFiles.Handler))
	v1 := r.Group("/api/v1")
	{
		//注册路由
		r.POST("/signup", controllers.SignUpHandler)
		r.POST("/login", controllers.LoginHandler)
		v1.Use(middlewares.JWTAuthMiddleware())
		{
			//社区内容添加
			v1.POST("addCommunityHandler", controllers.AddCommunityHandler)
			v1.GET("editCommunity/:id", controllers.EditCommunityCategory)
			v1.GET("communityList", controllers.CommunityList)
			v1.GET("communityList/:id", controllers.CommunityDetailId)

			//post内容
			// 创建post
			v1.POST("post", controllers.CreatePostHandler)
			//post/community 信息列表
			v1.GET("postList", controllers.PostListHandler)
			//帖子详情：
			v1.GET("post/:id", controllers.PostDetailHandler)
			v1.GET("postAll", controllers.PostAllHandler)
			v1.POST("vote", controllers.PostVoteHandler) // 投票
			// 根据时间或者分数排序分页展示帖子列表
			v1.GET("postsIndexList", controllers.PostsIndexListHandler)
			//通过社区查询到对应的帖子
			//v1.GET("community", controllers.GetCommunityPostListHandler)
			v1.POST("comment", controllers.CommentHandler)
			v1.GET("comment", controllers.CommentListHandler)
			v1.GET("postPrevious", controllers.PostPreviousHandler)
			v1.POST("createAdmin", controllers.CreateAdminHandler)
			v1.POST("createGroup", controllers.CreateGroupHandler)

		}
	}
	r.NoRoute(func(c *gin.Context) {
		c.String(http.StatusNotFound, "暂无数据!")
	})
	return r

}
